package cn.itsource.pet.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.query.SearchMasterMsgQuery;
import cn.itsource.pet.service.ISearchMasterMsgService;
import cn.itsource.user.domain.LoginInfo;
import cn.itsource.user.domain.User;
import cn.itsource.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.management.Query;
import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgCotroller {
    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IUserService userService;

    @PutMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        //在保存之前，需要 获取当前的系统用户 user
        /*
            前提：
              1.redis里面存储的有当前的登录用户-loginInfo
              2.令牌token每次请求头里面

         */
        //根据请求头[U-TOKEN]获取 token  HttpRequestServlet
        try {
            //获取请求头 token
            String token = request.getHeader("U-TOKEN");
            //根据token获取redis里面的数据
            LoginInfo loginInfo = (LoginInfo) redisTemplate.opsForValue().get(token);
            //根据loginInfo 获取User
            User user = userService.loadByLoginId(loginInfo.getId());
            //设置关联对象
            searchMasterMsg.setUser(user);
            //调用后台的添加数据---》计算离用户最近的门店
            searchMasterMsgService.add(searchMasterMsg);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }


    //分页查询
    @PostMapping
    public PageList<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery query){
        //获取当前的系统登录用户--loginInfo--employee
        //查询t_shop 表根据 employee id  ---Shop
        query.setShopId(27L);//本来时动态
        return searchMasterMsgService.queryPage(query);
    }

    @DeleteMapping("/{id}")
    public AjaxResult del( @PathVariable  Long id){

        searchMasterMsgService.msgToPet(id);

        return  AjaxResult.me();
    }
}
